<?php 
    //print_r($_SERVER);
    session_start();
    include_once 'inc/db.class.php';
    include_once 'inc/function.php';
    include_once 'inc/page.class.php';
    //print_r($_SERVER);
   
    $where=1;
    $bkey=isset($_REQUEST["bkey"]) ? $_REQUEST["bkey"] : "";
    //print_r($bkey);
    if(isset($_GET["id1"])){
        //通过一级获取二级
        $str="";
        foreach (getcategory($_GET["id1"]) as $v){
            foreach (getcategory($v["id"]) as $v3){
                $str.=$v3["id"].",";
            }
        }
        $str=substr($str, 0,-1);
        $where.=" and cid in(".$str.")";
    }
    if(isset($_GET["id2"])){//19
        //通过二级id获取三级id
        $str="";
        foreach (getcategory($_GET["id2"]) as $v){
            $str.=$v["id"].","; //63,64,
        }
        $str=substr($str,0,-1); //63,64
        $where.=" and cid in(".$str.")"; //cid in(63,64)
    }
    if(isset($_GET["id3"])){
        $where.=" and cid=".$_GET["id3"];
    }
    if(isset($_REQUEST["goodskey"])){
        $s=str_replace("'", "", $_REQUEST["goodskey"]);
        $where.=" and name like '%".$s."%'";
        $bkey=$s;
        $url=$_SERVER["HTTP_HOST"]."/myshop/search.php?bkey=".$s."&goodskey=".$s;
        $para="bkey=".$s."&goodskey=".$s;
    }else{
        $url=$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
        //www.myshop.com/search.php?id1=1&bkey=家用电器&bid=3
        //www.myshop.com/search.php?id1=1&bkey=家用电器&bid=3&page=5
        /**在当前URL中 删除掉 &bid=3**/
        //查询参数bid索引位置
        $p1=strrpos($url,"&bid");
        if($p1){
            $p2=strrpos($url,"&");
            //获取bid前面所有字符串
            $str1=substr($url,0,$p1);
            
            //判断&bid后面是否还有其他参数
            if($p1==$p2){
                //&bid后没有其他参数
                $str2="";
            }else{
                //&bid后还有其他参数
                $str2=substr($url, $p2);
            }
            
            $url=$str1.$str2;
        }
        
        /**过滤掉PAGE**/
        //$_SERVER["QUERY_STRING"]
        $para=$_SERVER["QUERY_STRING"];
        // id1=1&bkey=家用电器
        // id1=1&bkey=家用电器&page=2
        if($para){
            $queryarr=explode("&", $_SERVER["QUERY_STRING"]);
            //array(0=>"id1=1",1=>"bkey=家用电器");
            foreach($queryarr as $v){
                if($v){
                    $arr=explode("=", $v);
                    $queryarr2[$arr[0]]=$arr[1];
                }
                
            }
            //$queryarr2=array("id1"=>1,bkey=>"家用电器");
            $para="";
            foreach ($queryarr2 as $k=>$v){
                if($k!="page"){
                    $para.=$k."=".$v."&";
                }
            }
            $para=substr($para, 0,-1);
        }
        
        
    }
    
    
    
    
    
    //接收品牌id
    if(isset($_GET["bid"])){
        $where.=" and bid=".$_GET["bid"];
    }
    
    $rsb=array();
    //读取品牌
    if($bkey!=""){
        $sqlb="select * from brand where brandkey like '%".$bkey."%'";
        //print_r($sqlb);
        $rsb=$db->getData($sqlb);
    }
    
   
    //读取掌柜推荐产品
    $sqlt="select goods.id,goods.name,goods.price,goodspic.pic from goods,goodspic where goods.id=goodspic.gid and type=1 group by goods.id order by rand() limit 4";
    $rst=$db->getData($sqlt);
   // print_r($rst);
   
    //读取总记录条数
    $sqlcount="select count(-1) as c from goods where ".$where;
    $rscount=$db->getData($sqlcount,true);
    //当前页
    $page=isset($_GET["page"]) ? $_GET["page"] : 1;
    //每页条数
    $pagesize=16;
    //开始索引
    $bindex=($page-1)*$pagesize;
    
    
    $sql="select goods.id,goods.name,goods.price,goodspic.pic from goods,goodspic where ".$where ." and goods.id=goodspic.gid group by goods.id limit ".$bindex.",".$pagesize;
    $rs=$db->getData($sql);
    //print_r($rs);
    //echo $sql;
    
    $web=getwebinfo(); 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<?php include_once 'meta.php'; ?>

		<link href="assets/css/amazeui.css" rel="stylesheet" type="text/css" />
		<link href="assets/css/admin.css" rel="stylesheet" type="text/css" />

		<meta name="description" content="<?php echo $web["description"]; ?>" />
		<meta name="keywords" content="<?php echo $web["keywords"]; ?>" />
		<title><?php echo $web["name"] ?></title>
		<link href="basic/css/demo.css" rel="stylesheet" type="text/css" />

		<link href="css/seastyle.css" rel="stylesheet" type="text/css" />
		<link href="css/page.css" rel="stylesheet" type="text/css" />

		<script src="http://www.5imoban.net/download/jquery/jquery.1.11.3.js"></script>
		<script type="text/javascript" src="js/script.js"></script>
	</head>

	<body>

		<?php include_once 'header1.php'; ?>

			<div class="clear"></div>
			<b class="line"></b>
           <div class="search">
			<div class="search-list">
			<div class="nav-table">
					   <?php include_once 'header2.php'; ?>
			</div>
			
				
					<div class="am-g am-g-fixed">
						<div class="am-u-sm-12 am-u-md-12">
	                  	<div class="theme-popover">														
							
							<ul class="select">
								<p class="title font-normal">
									<span class="fl"><?php echo isset($s) ? $s : ""; ?></span>
									<span class="total fl">搜索到<strong class="num"><?php echo $rscount["c"]; ?></strong>件相关商品</span>
								</p>
								<div class="clear"></div>
								<li class="select-result">
									<dl>
										<dt>已选</dt>
										<dd class="select-no"></dd>
										<p class="eliminateCriteria">清除</p>
									</dl>
								</li>
								<div class="clear"></div>
								<li class="select-list">
									<dl id="select1">
										<dt class="am-badge am-round">品牌</dt>	
									
										 <div class="dd-conent">										
											<dd class="select-all <?php $selectbid=isset($_REQUEST["bid"]) ? $_REQUEST["bid"] : ""; if($selectbid==""){echo "selected";} ?>"><a href="http://<?php echo $url; ?>">全部</a></dd>
											<?php foreach ($rsb as $v){ ?>
											<dd class="<?php  if($v["id"]==$selectbid){echo "selected";} ?>"><a href="http://<?php echo $url; ?>&bid=<?php echo $v["id"]; ?>"><?php echo $v["name"]; ?></a></dd>
											<?php } ?>
										 </div>
						
									</dl>
								</li>
								
								
							</ul>
							<div class="clear"></div>
                        </div>
							<div class="search-content">
								<div class="sort">
									<li class="first"><a title="综合">综合排序</a></li>
									<li><a title="销量">销量排序</a></li>
									<li><a title="价格">价格优先</a></li>
									<li class="big"><a title="评价" href="#">评价为主</a></li>
								</div>
								<div class="clear"></div>

								<ul class="am-avg-sm-2 am-avg-md-3 am-avg-lg-4 boxes">
									<?php foreach ($rs as $v){ ?>
									<li>
										<div class="i-pic limit">
											<a href="introduction.php?id=<?php echo $v["id"]; ?>"><img height="200" src="goodsimg/<?php echo $v["pic"]; ?>" /></a>											
											<p title="<?php echo $v["name"]; ?>" class="title fl"><a href="introduction.php?id=<?php echo $v["id"]; ?>"><?php echo $v["name"]; ?></a></p>
											<p class="price fl">
												<b>¥</b>
												<strong><?php echo number_format($v["price"],2); ?></strong>
											</p>
											<p class="number fl">
												销量<span>1110</span>
											</p>
										</div>
									</li>
									<?php } ?>
								</ul>
							</div>
							<div class="search-side">

								<div class="side-title">
									掌柜推荐
								</div>
                                <?php foreach ($rst as $v){ ?>
								<li>
									<div class="i-pic check">
										<img src="goodsimg/<?php echo $v["pic"]; ?>" />
										<p style="overflow:hidden;white-space:nowrap;text-overflow:ellipsis;" class="check-title"><?php echo $v["name"]; ?></p>
										<p class="price fl">
											<b>¥</b>
											<strong><?php echo number_format($v["price"]); ?></strong>
										</p>
										<p class="number fl">
											销量<span>1110</span>
										</p>
									</div>
								</li>
								<?php } ?>

							</div>
							<div class="clear"></div>
							<!--分页 -->
							<?php 
							     $mypage=new page($rscount["c"],$pagesize,$page,"search.php?".$para."&page={page}");
							     echo $mypage->myde_write();
							?>
						</div>
					</div>
					<?php include_once 'footer.php';?>
				</div>

			</div>

		<!--引导 -->
		<div class="navCir">
			<li><a href="home2.html"><i class="am-icon-home "></i>首页</a></li>
			<li><a href="sort.html"><i class="am-icon-list"></i>分类</a></li>
			<li><a href="shopcart.html"><i class="am-icon-shopping-basket"></i>购物车</a></li>	
			<li><a href="person/index.html"><i class="am-icon-user"></i>我的</a></li>					
		</div>

		<!--菜单 -->
		<?php include_once 'aside.php'; ?>
		<script>
			window.jQuery || document.write('<script src="basic/js/jquery-1.9.min.js"><\/script>');
		</script>
		<script type="text/javascript" src="basic/js/quick_links.js"></script>

<div class="theme-popover-mask"></div>

	</body>

</html>